package com.example.jdimust.historydata;

import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.jdimust.R;
import com.example.jdimust.login.LoginActivity;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: MaZongyang
 * Date: 2021/5/20/0020 16:19
 * Description:
 */
public class HisDataActivity extends AppCompatActivity {

    private List<HisData> HisdataList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectCustomSlowCalls() //API等级11，使用StrictMode.noteSlowCode 自定义的耗时调用
                .detectDiskReads() //磁盘读取操作
                .detectDiskWrites() //磁盘写入操作
                .detectNetwork()  //网络操作
                //  .penaltyDialog() //弹出违规提示对话框
                .detectAll()
                .penaltyLog() //在Logcat 中打印违规异常信息
                .build());

        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectAll()
                .detectActivityLeaks()  //Activity泄露
                .detectLeakedSqlLiteObjects()  //泄露的Sqlite对象
                .setClassInstanceLimit(this.getClass(), 1) //检测实例数量
                .penaltyLog()
                .build());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hisdata);

        Intent intent = getIntent();
        String olddata = intent.getStringExtra("todata");
//        String jiaobiao = intent.getStringExtra("i");
        System.out.println("已进入HisDataActivity页面");
//        System.out.println("\n--------------------------------------------------\n" + olddata + "\n--------------------------------------------------\n");
        Gson gson3 =new Gson();
        String test = ("chaojimali");
//        olddata = olddata.replace("[", "");
//        olddata = olddata.replace("]", "");
//        System.out.println(olddata);
        HisDataAdapter adapter3 = new HisDataAdapter(HisDataActivity.this, R.layout.hisdata_item, HisdataList);
        try {
            JSONArray jsonarray = new JSONArray(olddata);
            for (int i = 0; i < jsonarray.length(); i++) {
                JSONObject jsonobj = jsonarray.getJSONObject(i);
                String vertemp = jsonobj.getString("version");
                String valtemp = jsonobj.getString("value");
                System.out.println("---版本---"+vertemp+"---数据---"+valtemp);
                HisData hisdata = new HisData(vertemp,valtemp);
                HisdataList.add(hisdata);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        LinearLayout mainLinerLayout = (LinearLayout) this.findViewById(R.id.ly_hisdata);
        TextView textview=new TextView(this);
        textview.setText("你好！");
        mainLinerLayout.addView(textview);
        ListView listView9 = (ListView) findViewById(R.id.lv_olddatalv);
        listView9.setAdapter(adapter3);
//        Type type = new TypeToken<List<ValueVersionKeyBean>>(){}.getType();
//        List s = gson3.fromJson(olddata, type);
//        for (int i = 0; i < valverkey.getValue().length(); i++) {
//
//        }
//        System.out.println(valverkeybean.);
//        System.out.println("\n------------------------------\n"+valverkeybean.getVersion().toString()+"\n------------------------------\n"+valverkeybean.getValue().toString());
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return true;
    }
    /**
     *菜单的点击事件
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case R.id.chageuser:
                Toast.makeText(this, "正在切换用户！", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(HisDataActivity.this, LoginActivity.class);//从SpendingActivity页面跳转至ExpenseProcesActivity页面
                startActivity(intent);
                break;
            default:
                break;
        }

        return true;
    }
}